import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Put,
  Query,
  Req,
} from '@nestjs/common';
import { DictService } from './dict.service';
import { Request } from 'express';
import { Post as PostEntity } from '../../entities/post.entity';
import { User } from '../../entities/user.entity';
import {
  ApiBearerAuth,
  ApiBody,
  ApiOperation,
  ApiTags,
  ApiParam,
  ApiOkResponse,
} from '@nestjs/swagger';
import { PostRegisterDto } from '../../dto/post/post-register.dto';
import { Public } from '../../common/decorators/public.decorator';
import { Dict } from '../../entities/dict.entity';
import { PaginatedResponseDto } from '../../common/class/res.class';
import { PageSearchRoleDto } from '../../dto/role/role.dto';
import {
  DictItemDto,
  PageDictDto,
  PageDictItemtDto,
} from '../../dto/dict/dict.dto';
import { DictItem } from '../../entities/dict-item.entity';

@ApiTags('数据字典')
@ApiBearerAuth()
@Controller('dict')
export class DictController {
  constructor(private readonly dictService: DictService) {}

  @ApiOperation({ summary: '新增字典' })
  @ApiBody({ type: Dict, description: '输入字典的各种信息' })
  @Post('createDict')
  async createDict(@Body() dictListDto: Dict): Promise<any> {
    const res = await this.dictService.createDict(dictListDto);
    return {
      code: 200,
      msg: '新增成功',
      data: null,
    };
  }

  @ApiOperation({ summary: '修改字典' })
  @ApiBody({ type: Dict, description: '输入字典的各种信息' })
  @Post('updateDict')
  async updateDict(@Body() dictListDto: Dict): Promise<any> {
    const res = await this.dictService.updateDict(dictListDto);
    return {
      code: 200,
      msg: '修改成功',
      data: null,
    };
  }

  @ApiOperation({ summary: '删除字典' })
  @Delete('deleteDict')
  async deleteDict(@Body() obj: Dict): Promise<any> {
    const res = await this.dictService.deleteDict(obj);
    return {
      code: 200,
      msg: '删除成功',
      data: res,
    };
  }

  @ApiOperation({ summary: '分页条件字典' })
  @ApiOkResponse({ type: PageDictDto, description: '分页查询结果' })
  @Post('page')
  async pageDict(
    @Body() dto: PageDictDto,
  ): Promise<PaginatedResponseDto<Dict>> {
    const [records, total] = await this.dictService.getDictsWithPageOptions(
      dto,
    );
    return {
      msg: '查询成功',
      data: {
        records,
        pageSize: dto.pageSize,
        pageNo: dto.pageNo,
        total,
      },
    };
  }
  @ApiOperation({ summary: '分页条件字典' })
  @ApiOkResponse({ type: PageDictDto, description: '分页查询结果' })
  @Get('getDictItems/:dictCode')
  async getDictItems(@Param('dictCode') dictCode: string): Promise<DictItem[]> {
    return await this.dictService.getDictItemsByDictCode(dictCode);
  }

  @ApiOperation({ summary: '新增字典配置' })
  @ApiBody({ type: DictItemDto, description: '输入字典配置的各种信息' })
  @Post('createDictItem')
  async createDictItem(@Body() dictListDto: DictItemDto): Promise<any> {
    const res = await this.dictService.createDictItem(dictListDto);
    return {
      code: 200,
      msg: '新增成功',
      data: null,
    };
  }

  @ApiOperation({ summary: '修改字典配置' })
  @ApiBody({ type: DictItemDto, description: '输入字典的各种信息' })
  @Post('updateDictItem')
  async updateDictItem(@Body() dictListDto: DictItemDto): Promise<any> {
    const res = await this.dictService.updateDictItem(dictListDto);
    return {
      code: 200,
      msg: '修改成功',
      data: null,
    };
  }

  @ApiOperation({ summary: '删除字典配置' })
  @Post('deleteDictItem')
  async deleteDictItem(@Body() obj: DictItem): Promise<any> {
    const res = await this.dictService.deleteDictItem(obj);
    return {
      code: 200,
      msg: '删除成功',
      data: null,
    };
  }

  @ApiOperation({ summary: '分页条件字典配置' })
  @ApiOkResponse({ type: PageDictItemtDto, description: '分页查询结果' })
  @Post('pageItem')
  async pageDictItem(
    @Body() dto: PageDictItemtDto,
  ): Promise<PaginatedResponseDto<DictItem>> {
    const [records, total] = await this.dictService.getDictItemsWithPageOptions(
      dto,
    );
    return {
      msg: '查询成功',
      data: {
        records,
        pageSize: dto.pageSize,
        pageNo: dto.pageNo,
        total,
      },
    };
  }
}
